package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
// import com.github.pagehelper.PageHelper;
// import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.PageDto;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.enums.HttpEnum;
import com.itheima.reggie.mapper.EmployeeMapper;
import com.itheima.reggie.service.EmployeeService;
import com.itheima.reggie.utils.LoginThreadLocal;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
    /**
     * 分页条件列表
     *
     * @param dto
     * @return
     */
    @Override
    public R pageList(PageDto dto) {
        // 参数校验
        if (null == dto) {
            return R.error(HttpEnum.DATA_NULL.getCode(), HttpEnum.DATA_NULL.getMsg());
        }
        // 分页查询
        // 1.页码,条数判断
        dto.setPage(null == dto.getPage() ? 0 : dto.getPage());

        dto.setPageSize(null == dto.getPage() || dto.getPageSize() < 10 ? 10 : dto.getPageSize());

        EmployeeMapper employeeMapper = getBaseMapper();
        // 使用MP分页
         Page<Employee> employeePage = MPPage(dto, employeeMapper);
         return R.success(employeePage);
        // 使用PageHelper分页
        // Map<String, Object> data = byPageHelper(dto, employeeMapper);
        // return R.success(data);
    }

    /**
     * 员工新增
     *
     * @param employee
     * @return
     */
    @Override
    public R add(Employee employee) {
        // 参数校验
        if (null==employee) {
            return R.error(HttpEnum.DATA_NULL.getMsg());
        }
        log.info("新增员工，员工信息：{}",employee.toString());

        // 密码Md5加密 默认123456
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        // 创建人从当前线程局部中获取
        employee.setCreateUser(LoginThreadLocal.getCurrent()==null?null:LoginThreadLocal.getCurrent());
        employee.setUpdateUser(LoginThreadLocal.getCurrent()==null?null:LoginThreadLocal.getCurrent());

        save(employee);
        return R.success("新增员工成功!");
    }

    @Override
    public R get(Long id) {
        if (id==null) {
            return R.error(HttpEnum.PARAMETER_NULL.getMsg());
        }
        Employee byId = getById(id);
        if (byId==null) {
            return R.error("该员工不存在");
        }
        return R.success(byId);
    }

    /**
     * PageHelper分页
     *
     * @param dto
     * @param employeeMapper
     * @return
     */
    // private static Map<String, Object> byPageHelper(PageDto dto, EmployeeMapper employeeMapper) {
    //     PageHelper.startPage(dto.getPage(), dto.getPageSize());
    //
    //     LambdaQueryWrapper<Employee> queryWrapper = Wrappers.<Employee>lambdaQuery()
    //             .like(StringUtils.isNotBlank(dto.getName()), Employee::getName, dto.getName())
    //             .orderByDesc(Employee::getUpdateTime);
    //     List<Employee> employeeList = employeeMapper.selectList(queryWrapper);
    //
    //     PageInfo<Employee> employeePageInfo = new PageInfo<>(employeeList);
    //     Map<String, Object> data = new HashMap<String, Object>() {{
    //         put("total", employeePageInfo.getTotal());
    //         put("records", employeePageInfo.getList());
    //     }};
    //     return data;
    // }

    /**
     * MP自带分页插件
     *
     * @param dto
     * @param employeeMapper
     * @return
     */
    private static Page<Employee> MPPage(PageDto dto, EmployeeMapper employeeMapper) {
        // MP自带分页插件
        Page<Employee> page = new Page<>(dto.getPage(), dto.getPageSize());

        LambdaQueryWrapper<Employee> empWrapper = Wrappers.<Employee>lambdaQuery()
                // 名称模糊 判空
                .like(StringUtils.isNotBlank(dto.getName()), Employee::getName, dto.getName())
                // 更新时间倒序排序
                .orderByDesc(Employee::getUpdateTime);

        Page<Employee> employeePage = employeeMapper.selectPage(page, empWrapper);
        return employeePage;
    }
}
